home *** CD-ROM | disk | FTP | other *** search
/ CDROM of CDROMs 1994 May / The CD-ROM of CD-ROMs - The Consumer's Guide to CD-ROMs - Walnut Creek May 1994.iso / samples / unix / part3 < prev    next >
Encoding:
Internet Message Format  |  1994-04-12  |  30.1 KB

  1. From: tmatimar@isgtec.com (Ted M A Timar)
  2. Newsgroups: comp.unix.questions,comp.unix.shell,news.answers,comp.answers
  3. Subject: Unix - Frequently Asked Questions (3/7) [Frequent posting]
  4.  
  5. Archive-name: unix-faq/faq/part3
  6. Version: $Id: part3,v 2.2 1993/03/18 23:06:21 tmatimar Exp $
  7.  
  8. These seven articles contain the answers to some Frequently Asked
  9. Questions often seen in comp.unix.questions and comp.unix.shell.
  10. Please don't ask these questions again, they've been answered plenty
  11. of times already - and please don't flame someone just because they may
  12. not have read this particular posting.  Thank you.
  13.  
  14. Many FAQs, including this one, are available on the archive site
  15. rtfm.mit.edu (18.172.1.27) in the directory pub/usenet/news.answers.
  16. The name under which a FAQ is archived appears in the "Archive-Name:"
  17. line at the top of the article.  This FAQ is archived as
  18. "unix-faq/faq/part[1-7]".
  19.  
  20. These articles are divided approximately as follows:
  21.  
  22.       1.*) General questions.
  23.       2.*) Relatively basic questions, likely to be asked by beginners.
  24.       3.*) Intermediate questions.
  25.       4.*) Advanced questions, likely to be asked by people who thought
  26.        they already knew all of the answers.
  27.       5.*) Questions pertaining to the various shells, and the differences.
  28.       6.*) An overview of Unix variants.
  29.       7.*) An comparison of configuration management systems (RCS, SCCS).
  30.  
  31. This article includes answers to:
  32.  
  33.       3.1)  How do I find the creation time of a file?
  34.       3.2)  How do I use "rsh" without having the rsh hang around
  35.               until the remote command has completed?
  36.       3.3)  How do I truncate a file?
  37.       3.4)  Why doesn't find's "{}" symbol do what I want?
  38.       3.5)  How do I set the permissions on a symbolic link?
  39.       3.6)  How do I "undelete" a file?
  40.       3.7)  How can a process detect if it's running in the background?
  41.       3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  42.       3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  43.               programs from a shell script or in the background?
  44.       3.10) How do I find the process ID of a program with a particular
  45.             name from inside a shell script or C program?
  46.       3.11) How do I check the exit status of a remote command
  47.             executed via "rsh" ?
  48.       3.12) Is it possible to pass shell variable settings into an awk program?
  49.       3.13) How do I get rid of zombie processes that persevere?
  50.       3.14) How do I get lines from a pipe as they are written instead of
  51.             only in larger blocks.
  52.  
  53. If you're looking for the answer to, say, question 3.5, and want to skip
  54. everything else, you can search ahead for the regular expression "^3.5)".
  55.  
  56. While these are all legitimate questions, they seem to crop up in
  57. comp.unix.questions or comp.unix.shell on an annual basis, usually
  58. followed by plenty of replies (only some of which are correct) and then
  59. a period of griping about how the same questions keep coming up.  You
  60. may also like to read the monthly article "Answers to Frequently Asked
  61. Questions" in the newsgroup "news.announce.newusers", which will tell
  62. you what "UNIX" stands for.
  63.  
  64. With the variety of Unix systems in the world, it's hard to guarantee
  65. that these answers will work everywhere.  Read your local manual pages
  66. before trying anything suggested here.  If you have suggestions or
  67. corrections for any of these answers, please send them to to
  68. tmatimar@isgtec.com.
  69.  
  70. ----------------------------------------------------------------------
  71.  
  72. Subject: How do I find the creation time of a file?
  73. Date: Thu Mar 18 17:16:55 EST 1993
  74.  
  75. 3.1)  How do I find the creation time of a file?
  76.  
  77.       You can't - it isn't stored anywhere.  Files have a last-modified
  78.       time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
  79.       and an inode change time (shown by "ls -lc"). The latter is often
  80.       referred to as the "creation time" - even in some man pages -
  81.       but that's wrong; it's also set by such operations as mv, ln,
  82.       chmod, chown and chgrp.
  83.  
  84.       The man page for "stat(2)" discusses this.
  85.  
  86. ------------------------------
  87.  
  88. Subject: How do I use "rsh" without having the rsh hang around ... ?
  89. Date: Thu Mar 18 17:16:55 EST 1993
  90.  
  91. 3.2)  How do I use "rsh" without having the rsh hang around until the
  92.       remote command has completed?
  93.  
  94.       (See note in question 2.7 about what "rsh" we're talking about.)
  95.  
  96.       The obvious answers fail:
  97.               rsh machine command &
  98.       or      rsh machine 'command &'
  99.  
  100.       For instance, try doing   rsh machine 'sleep 60 &' and you'll see
  101.       that the 'rsh' won't exit right away.  It will wait 60 seconds
  102.       until the remote 'sleep' command finishes, even though that
  103.       command was started in the background on the remote machine.  So
  104.       how do you get the 'rsh' to exit immediately after the 'sleep' is
  105.       started?
  106.  
  107.       The solution - if you use csh on the remote machine:
  108.  
  109.         rsh machine -n 'command >&/dev/null </dev/null &'
  110.  
  111.       If you use sh on the remote machine:
  112.  
  113.         rsh machine -n 'command >/dev/null 2>&1 </dev/null &'
  114.  
  115.       Why?  "-n" attaches rsh's stdin to /dev/null so you could run the
  116.       complete rsh command in the background on the LOCAL machine.
  117.       Thus "-n" is equivalent to another specific "< /dev/null".
  118.       Furthermore, the input/output redirections on the REMOTE machine
  119.       (inside the single quotes) ensure that rsh thinks the session can
  120.       be terminated (there's no data flow any more.)
  121.  
  122.       Note: The file that you redirect to/from on the remote machine
  123.       doesn't have to be /dev/null; any ordinary file will do.
  124.  
  125.       In many cases, various parts of these complicated commands
  126.       aren't necessary.
  127.  
  128. ------------------------------
  129.  
  130. Subject: How do I truncate a file?
  131. Date: Thu Mar 18 17:16:55 EST 1993
  132.  
  133. 3.3)  How do I truncate a file?
  134.  
  135.       The BSD function ftruncate() sets the length of a file.  Xenix -
  136.       and therefore SysV r3.2 and later - has the chsize() system
  137.       call.  For other systems, the only kind of truncation you can do
  138.       is truncation to length zero with creat() or open(..., O_TRUNC).
  139.  
  140. ------------------------------
  141.  
  142. Subject: Why doesn't find's "{}" symbol do what I want?
  143. Date: Thu Mar 18 17:16:55 EST 1993
  144.  
  145. 3.4)  Why doesn't find's "{}" symbol do what I want?
  146.  
  147.       "find" has a -exec option that will execute a particular command
  148.       on all the selected files. Find will replace any "{}" it sees
  149.       with the name of the file currently under consideration.
  150.  
  151.       So, some day you might try to use "find" to run a command on
  152.       every file, one directory at a time.  You might try this:
  153.  
  154.     find /path -type d -exec command {}/\* \;
  155.  
  156.       hoping that find will execute, in turn
  157.  
  158.     command directory1/*
  159.     command directory2/*
  160.     ...
  161.  
  162.       Unfortunately, find only expands the "{}" token when it appears
  163.       by itself.  Find will leave anything else like "{}/*" alone, so
  164.       instead of doing what you want, it will do
  165.  
  166.     command {}/*
  167.     command {}/*
  168.     ...
  169.  
  170.       once for each directory.  This might be a bug, it might be a
  171.       feature, but we're stuck with the current behaviour.
  172.  
  173.       So how do you get around this?  One way would be to write a
  174.       trivial little shell script, let's say "./doit", that consists of
  175.  
  176.     command "$1"/*
  177.  
  178.       You could then use
  179.  
  180.     find /path -type d -exec ./doit {} \;
  181.  
  182.       Or if you want to avoid the "./doit" shell script, you can use
  183.  
  184.     find /path -type d -exec sh -c 'command $0/*' {} \;
  185.  
  186.       (This works because within the 'command' of "sh -c 'command' A B C ...",
  187.        $0 expands to A, $1 to B, and so on.)
  188.  
  189.       or you can use the construct-a-command-with-sed trick
  190.  
  191.     find /path -type d -print | sed 's:.*:command &/*:' | sh
  192.  
  193.       If all you're trying to do is cut down on the number of times
  194.       that "command" is executed, you should see if your system has the
  195.       "xargs" command.  Xargs reads arguments one line at a time from
  196.       the standard input and assembles as many of them as will fit into
  197.       one command line.  You could use
  198.  
  199.     find /path -print | xargs command
  200.  
  201.       which would result in one or more executions of
  202.  
  203.     command file1 file2 file3 file4 dir1/file1 dir1/file2
  204.  
  205.       Unfortunately this is not a perfectly robust or secure solution.
  206.       Xargs expects its input lines to be terminated with newlines, so
  207.       it will be confused by files with odd characters such as newlines
  208.       in their names.
  209.  
  210. ------------------------------
  211.  
  212. Subject: How do I set the permissions on a symbolic link?
  213. Date: Thu Mar 18 17:16:55 EST 1993
  214.  
  215. 3.5)  How do I set the permissions on a symbolic link?
  216.  
  217.       Permissions on a symbolic link don't really mean anything.  The
  218.       only permissions that count are the permissions on the file that
  219.       the link points to.
  220.  
  221. ------------------------------
  222.  
  223. Subject: How do I "undelete" a file?
  224. Date: Thu Mar 18 17:16:55 EST 1993
  225.  
  226. 3.6)  How do I "undelete" a file?
  227.  
  228.       Someday, you are going to accidentally type something like
  229.       "rm * .foo", and find you just deleted "*" instead of "*.foo".
  230.       Consider it a rite of passage.
  231.  
  232.       Of course, any decent systems administrator should be doing
  233.       regular backups.  Check with your sysadmin to see if a recent
  234.       backup copy of your file is available.  But if it isn't, read
  235.       on.
  236.  
  237.       For all intents and purposes, when you delete a file with "rm" it
  238.       is gone.  Once you "rm" a file, the system totally forgets which
  239.       blocks scattered around the disk comprised your file.  Even
  240.       worse, the blocks from the file you just deleted are going to be
  241.       the first ones taken and scribbled upon when the system needs
  242.       more disk space.  However, never say never.  It is theoretically
  243.       possible *if* you shut down the system immediately after the "rm"
  244.       to recover portions of the data.  However, you had better have a
  245.       very wizardly type person at hand with hours or days to spare to
  246.       get it all back.
  247.  
  248.       Your first reaction when you "rm" a file by mistake is why not
  249.       make a shell alias or procedure which changes "rm" to move files
  250.       into a trash bin rather than delete them?  That way you can
  251.       recover them if you make a mistake, and periodically clean out
  252.       your trash bin.  Two points:  first, this is generally accepted
  253.       as a *bad* idea.  You will become dependent upon this behaviour
  254.       of "rm", and you will find yourself someday on a normal system
  255.       where "rm" is really "rm", and you will get yourself in trouble.
  256.       Second, you will eventually find that the hassle of dealing with
  257.       the disk space and time involved in maintaining the trash bin, it
  258.       might be easier just to be a bit more careful with "rm".  For
  259.       starters, you should look up the "-i" option to "rm" in your
  260.       manual.
  261.  
  262.       If you are still undaunted, then here is a possible simple
  263.       answer.  You can create yourself a "can" command which moves
  264.       files into a trashcan directory. In csh(1) you can place the
  265.       following commands in the ".login" file in your home directory:
  266.  
  267.     alias can    'mv \!* ~/.trashcan'       # junk file(s) to trashcan
  268.     alias mtcan    'rm -f ~/.trashcan/*'       # irretrievably empty trash
  269.     if ( ! -d ~/.trashcan ) mkdir ~/.trashcan  # ensure trashcan exists
  270.  
  271.       You might also want to put a:
  272.  
  273.     rm -f ~/.trashcan/*
  274.  
  275.       in the ".logout" file in your home directory to automatically
  276.       empty the trash when you log out.  (sh and ksh versions are left
  277.       as an exercise for the reader.)
  278.  
  279.       MIT's Project Athena has produced a comprehensive
  280.       delete/undelete/expunge/purge package, which can serve as a
  281.       complete replacement for rm which allows file recovery.  This
  282.       package was posted to comp.sources.misc (volume 17, issue
  283.       023-026)
  284.  
  285. ------------------------------
  286.  
  287. Subject: How can a process detect if it's running in the background?
  288. Date: Thu Mar 18 17:16:55 EST 1993
  289.  
  290. 3.7)  How can a process detect if it's running in the background?
  291.  
  292.       First of all: do you want to know if you're running in the
  293.       background, or if you're running interactively? If you're
  294.       deciding whether or not you should print prompts and the like,
  295.       that's probably a better criterion. Check if standard input
  296.       is a terminal:
  297.  
  298.         sh: if [ -t 0 ]; then ... fi
  299.         C: if(isatty(0)) { ... }
  300.  
  301.       In general, you can't tell if you're running in the background.
  302.       The fundamental problem is that different shells and different
  303.       versions of UNIX have different notions of what "foreground" and
  304.       "background" mean - and on the most common type of system with a
  305.       better-defined notion of what they mean, programs can be moved
  306.       arbitrarily between foreground and background!
  307.  
  308.       UNIX systems without job control typically put a process into the
  309.       background by ignoring SIGINT and SIGQUIT and redirecting the
  310.       standard input to "/dev/null"; this is done by the shell.
  311.  
  312.       Shells that support job control, on UNIX systems that support job
  313.       control, put a process into the background by giving it a process
  314.       group ID different from the process group to which the terminal
  315.       belongs.  They move it back into the foreground by setting the
  316.       terminal's process group ID to that of the process.  Shells that
  317.       do *not* support job control, on UNIX systems that support job
  318.       control, typically do what shells do on systems that don't
  319.       support job control.
  320.  
  321. ------------------------------
  322.  
  323. Subject: Why doesn't redirecting a loop work as intended?  (Bourne shell)
  324. Date: Thu Mar 18 17:16:55 EST 1993
  325.  
  326. 3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  327.  
  328.       Take the following example:
  329.  
  330.     foo=bar
  331.  
  332.     while read line
  333.     do
  334.         # do something with $line
  335.         foo=bletch
  336.     done < /etc/passwd
  337.  
  338.     echo "foo is now: $foo"
  339.  
  340.       Despite the assignment ``foo=bletch'' this will print
  341.       ``foo is now: bar'' in many implementations of the Bourne shell.
  342.       Why?  Because of the following, often undocumented, feature of
  343.       historic Bourne shells: redirecting a control structure (such as
  344.       a loop, or an ``if'' statement) causes a subshell to be created,
  345.       in which the structure is executed; variables set in that
  346.       subshell (like the ``foo=bletch'' assignment) don't affect the
  347.       current shell, of course.
  348.  
  349.       The POSIX 1003.2 Shell and Tools Interface standardization
  350.       committee forbids the behaviour described above, i.e. in P1003.2
  351.       conformant Bourne shells the example will print ``foo is now:
  352.       bletch''.
  353.  
  354.       In historic (and P1003.2 conformant) implementations you can use
  355.       the following `trick' to get around the redirection problem:
  356.  
  357.     foo=bar
  358.  
  359.     # make file descriptor 9 a duplicate of file descriptor 0 (stdin);
  360.     # then connect stdin to /etc/passwd; the original stdin is now
  361.     # `remembered' in file descriptor 9; see dup(2) and sh(1)
  362.     exec 9<&0 < /etc/passwd
  363.  
  364.     while read line
  365.     do
  366.         # do something with $line
  367.         foo=bletch
  368.     done
  369.  
  370.     # make stdin a duplicate of file descriptor 9, i.e. reconnect
  371.     # it to the original stdin; then close file descriptor 9
  372.     exec 0<&9 9<&-
  373.  
  374.     echo "foo is now: $foo"
  375.  
  376.       This should always print ``foo is now: bletch''.
  377.       Right, take the next example:
  378.  
  379.     foo=bar
  380.  
  381.     echo bletch | read foo
  382.  
  383.     echo "foo is now: $foo"
  384.  
  385.       This will print ``foo is now: bar'' in many implementations,
  386.       ``foo is now: bletch'' in some others.  Why?  Generally each part
  387.       of a pipeline is run in a different subshell; in some
  388.       implementations though, the last command in the pipeline is made
  389.       an exception: if it is a builtin command like ``read'', the
  390.       current shell will execute it, else another subshell is created.
  391.  
  392.       POSIX 1003.2 allows both behaviours so portable scripts cannot
  393.       depend on any of them.
  394.  
  395. ------------------------------
  396.  
  397. Subject: How do I run ... interactive programs from a shell script ... ?
  398. Date: Thu Mar 18 17:16:55 EST 1993
  399.  
  400. 3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  401.       programs from a shell script or in the background?
  402.  
  403.       These programs expect a terminal interface.  Shells makes no
  404.       special provisions to provide one.  Hence, such programs cannot
  405.       be automated in shell scripts.
  406.  
  407.       The 'expect' program provides a programmable terminal interface
  408.       for automating interaction with such programs.  The following
  409.       expect script is an example of a non-interactive version of
  410.       passwd(1).
  411.  
  412.     # username is passed as 1st arg, password as 2nd
  413.     set password [index $argv 2]
  414.     spawn passwd [index $argv 1]
  415.     expect "*password:"
  416.     send "$password\r"
  417.     expect "*password:"
  418.     send "$password\r"
  419.     expect eof
  420.  
  421.       expect can partially automate interaction which is especially
  422.       useful for telnet, rlogin, debuggers or other programs that have
  423.       no built-in command language.  The distribution provides an
  424.       example script to rerun rogue until a good starting configuration
  425.       appears.  Then, control is given back to the user to enjoy the game.
  426.  
  427.       Fortunately some programs have been written to manage the
  428.       connection to a pseudo-tty so that you can run these sorts of
  429.       programs in a script.
  430.  
  431.       To get expect, email "send pub/expect/expect.shar.Z" to
  432.       library@cme.nist.gov or anonymous ftp same from
  433.       durer.cme.nist.gov.
  434.  
  435.       Another solution is provided by the pty 4.0 program, which runs a
  436.       program under a pseudo-tty session and was posted to
  437.       comp.sources.unix, volume 25.  A pty-based solution using named
  438.       pipes to do the same as the above might look like this:
  439.  
  440.     #!/bin/sh
  441.     /etc/mknod out.$$ p; exec 2>&1
  442.     ( exec 4<out.$$; rm -f out.$$
  443.     <&4 waitfor 'password:'
  444.         echo "$2"
  445.     <&4 waitfor 'password:'
  446.         echo "$2"
  447.     <&4 cat >/dev/null
  448.     ) | ( pty passwd "$1" >out.$$ )
  449.  
  450.       Here, 'waitfor' is a simple C program that searches for
  451.       its argument in the input, character by character.
  452.  
  453.       A simpler pty solution (which has the drawback of not
  454.       synchronizing properly with the passwd program) is
  455.  
  456.     #!/bin/sh
  457.     ( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"
  458.  
  459. ------------------------------
  460.  
  461. Subject: How do I find the process ID of a program with a particular name ... ?
  462. Date: Thu Mar 18 17:16:55 EST 1993
  463.  
  464. 3.10) How do I find the process ID of a program with a particular name
  465.       from inside a shell script or C program?
  466.  
  467.       In a shell script:
  468.  
  469.       There is no utility specifically designed to map between program
  470.       names and process IDs.  Furthermore, such mappings are often
  471.       unreliable, since it's possible for more than one process to have
  472.       the same name, and since it's possible for a process to change
  473.       its name once it starts running.  However, a pipeline like this
  474.       can often be used to get a list of processes (owned by you) with
  475.       a particular name:
  476.  
  477.         ps ux | awk '/name/ && !/awk/ {print $2}'
  478.  
  479.       You replace "name" with the name of the process for which you are
  480.       searching.
  481.  
  482.       The general idea is to parse the output of ps, using awk or grep
  483.       or other utilities, to search for the lines with the specified
  484.       name on them, and print the PID's for those lines.  Note that the
  485.       "!/awk/" above prevents the awk process for being listed.
  486.  
  487.       You may have to change the arguments to ps, depending on what
  488.       kind of Unix you are using.
  489.  
  490.       In a C program:
  491.  
  492.       Just as there is no utility specifically designed to map between
  493.       program names and process IDs, there are no (portable) C library
  494.       functions to do it either.
  495.  
  496.       However, some vendors provide functions for reading Kernel
  497.       memory; for example, Sun provides the "kvm_" functions, and Data
  498.       General provides the "dg_" functions.  It may be possible for any
  499.       user to use these, or they may only be useable by the super-user
  500.       (or a user in group "kmem") if read-access to kernel memory on
  501.       your system is restricted.  Furthermore, these functions are
  502.       often not documented or documented badly, and might change from
  503.       release to release.
  504.  
  505.       Some vendors provide a "/proc" filesystem, which appears as a
  506.       directory with a bunch of filenames in it.  Each filename is a
  507.       number, corresponding to a process ID, and you can open the file
  508.       and read it to get information about the process.  Once again,
  509.       access to this may be restricted, and the interface to it may
  510.       change from system to system.
  511.  
  512.       If you can't use vendor-specific library functions, and you
  513.       don't have /proc, and you still want to do this completely
  514.       in C, you
  515.       are going to have to do the grovelling through kernel memory
  516.       yourself.  For a good example of how to do this on many systems,
  517.       see the sources to "ofiles", available in the comp.sources.unix
  518.       archives.  (A package named "kstuff" to help with kernel
  519.       grovelling was posted to alt.sources in May 1991 and is also
  520.       available via anonymous ftp as
  521.       usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  522.       wuarchive.wustl.edu.)
  523.  
  524. ------------------------------
  525.  
  526. Subject: How do I check the exit status of a remote command executed via "rsh"?
  527. Date: Thu Mar 18 17:16:55 EST 1993
  528.  
  529. 3.11) How do I check the exit status of a remote command
  530.       executed via "rsh" ?
  531.  
  532.       This doesn't work:
  533.  
  534.     rsh some-machine some-crummy-command || echo "Command failed"
  535.  
  536.       The exit status of 'rsh' is 0 (success) if the rsh program
  537.       itself completed successfully, which probably isn't what
  538.       you wanted.
  539.  
  540.       If you want to check on the exit status of the remote program,
  541.       you can try using Maarten Litmaath's 'ersh' script, which was
  542.       posted to alt.sources in January, 1991.  ersh is a shell script
  543.       that calls rsh, arranges for the remote machine to echo the
  544.       status of the command after it completes, and exits with that
  545.       status.
  546.  
  547. ------------------------------
  548.  
  549. Subject: Is it possible to pass shell variable settings into an awk program?
  550. Date: Thu Mar 18 17:16:55 EST 1993
  551.  
  552. 3.12) Is it possible to pass shell variable settings into an awk program?
  553.  
  554.       There are two different ways to do this.  The first involves
  555.       simply expanding the variable where it is needed in the program.
  556.       For example, to get a list of all ttys you're using:
  557.  
  558.     who | awk '/^'"$USER"'/ { print $2 }'                (1)
  559.  
  560.       Single quotes are usually used to enclose awk programs because
  561.       the character '$' is often used in them, and '$' will be
  562.       interpreted by the shell if enclosed inside double quotes, but
  563.       not if enclosed inside single quotes.  In this case, we *want*
  564.       the '$' in "$USER" to be interpreted by the shell, so we close
  565.       the single quotes and then put the "$USER" inside double quotes.
  566.       Note that there are no spaces in any of that, so the shell will
  567.       see it all as one argument.  Note, further, that the double
  568.       quotes probably aren't necessary in this particular case (i.e. we
  569.       could have done
  570.  
  571.     who | awk '/^'$USER'/ { print $2 }'                (2)
  572.  
  573.       ), but they should be included nevertheless because they are
  574.       necessary when the shell variable in question contains special
  575.       characters or spaces.
  576.  
  577.       The second way to pass variable settings into awk is to use an
  578.       often undocumented feature of awk which allows variable settings
  579.       to be specified as "fake file names" on the command line.  For
  580.       example:
  581.  
  582.     who | awk '$1 == user { print $2 }' user="$USER" -        (3)
  583.  
  584.       Variable settings take effect when they are encountered on the
  585.       command line, so, for example, you could instruct awk on how to
  586.       behave for different files using this technique.  For example:
  587.  
  588.     awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  589.  
  590.       Note that some versions of awk will cause variable settings
  591.       encountered before any real filenames to take effect before the
  592.       BEGIN block is executed, but some won't so neither way should be
  593.       relied upon.
  594.  
  595.       Note, further, that when you specify a variable setting, awk
  596.       won't automatically read from stdin if no real files are
  597.       specified, so you need to add a "-" argument to the end of your
  598.       command, as I did at (3) above.
  599.  
  600. ------------------------------
  601.  
  602. Subject: How do I get rid of zombie processes that persevere?
  603. From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  604. Date: Fri, 17 Jan 92 14:40:09 -0500
  605.  
  606. 3.13) How do I get rid of zombie processes that persevere?
  607.  
  608.       Unfortunately, it's impossible to generalize how the death of
  609.       child processes should behave, because the exact mechanism varies
  610.       over the various flavors of Unix.
  611.  
  612.       First of all, by default, you have to do a wait() for child
  613.       processes under ALL flavors of Unix.  That is, there is no flavor
  614.       of Unix that I know of that will automatically flush child
  615.       processes that exit, even if you don't do anything to tell it to
  616.       do so.
  617.  
  618.       Second, under some SysV-derived systems, if you do
  619.       "signal(SIGCHLD, SIG_IGN)" (well, actually, it may be SIGCLD
  620.       instead of SIGCHLD, but most of the newer SysV systems have
  621.       "#define SIGCHLD SIGCLD" in the header files), then child
  622.       processes will be cleaned up automatically, with no further
  623.       effort in your part.  The best way to find out if it works at
  624.       your site is to try it, although if you are trying to write
  625.       portable code, it's a bad idea to rely on this in any case.
  626.       Unfortunately, POSIX doesn't allow you to do this; the behavior
  627.       of setting the SIGCHLD to SIG_IGN under POSIX is undefined, so
  628.       you can't do it if your program is supposed to be
  629.       POSIX-compliant.
  630.  
  631.       If you can't use SIG_IGN to force automatic clean-up, then you've
  632.       got to write a signal handler to do it.  It isn't easy at all to
  633.       write a signal handler that does things right on all flavors of
  634.       Unix, because of the following inconsistencies:
  635.  
  636.       On some flavors of Unix, the SIGCHLD signal handler is called if
  637.       one *or more* children have died.  This means that if your signal
  638.       handler only does one wait() call, then it won't clean up all of
  639.       the children.  Fortunately, I believe that all Unix flavors for
  640.       which this is the case have available to the programmer the
  641.       wait3() call, which allows the WNOHANG option to check whether or
  642.       not there are any children waiting to be cleaned up.  Therefore,
  643.       on any system that has wait3(), your signal handler should call
  644.       wait3() over and over again with the WNOHANG option until there
  645.       are no children left to clean up.
  646.  
  647.       On SysV-derived systems, SIGCHLD signals are regenerated if there
  648.       are child processes still waiting to be cleaned up after you exit
  649.       the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  650.       systems to assume when the signal handler gets called that you
  651.       only have to clean up one signal, and assume that the handler
  652.       will get called again if there are more to clean up after it
  653.       exits.
  654.  
  655.       On older systems, signal handlers are automatically reset to
  656.       SIG_DFL when the signal handler gets called.  On such systems,
  657.       you have to put "signal(SIGCHILD, catcher_func)" (where
  658.       "catcher_func" is the name of the handler function) as the first
  659.       thing in the signal handler, so that it gets reset.
  660.       Unfortunately, there is a race condition which may cause you to
  661.       get a SIGCHLD signal and have it ignored between the time your
  662.       handler gets called and the time you reset the signal.
  663.       Fortunately, newer implementations of signal() don't reset the
  664.       handler to SIG_DFL when the handler function is called.  To get
  665.       around this problem, on systems that do not have wait3() but do
  666.       have SIGCLD, you need to reset the signal handler with a call to
  667.       signal() after doing at least one wait() within the handler, each
  668.       time it is called.
  669.  
  670.       The summary of all this is that on systems that have wait3(), you
  671.       should use that and your signal handler should loop, and on
  672.       systems that don't, you should have one call to wait() per
  673.       invocation of the signal handler.
  674.  
  675.       One more thing -- if you don't want to go through all of this
  676.       trouble, there is a portable way to avoid this problem, although
  677.       it is somewhat less efficient.  Your parent process should fork,
  678.       and then wait right there and then for the child process to
  679.       terminate.  The child process then forks again, giving you a
  680.       child and a grandchild.  The child exits immediately (and hence
  681.       the parent waiting for it notices its death and continues to
  682.       work), and the grandchild does whatever the child was originally
  683.       supposed to.  Since its parent died, it is inherited by init,
  684.       which will do whatever waiting is needed.  This method is
  685.       inefficient because it requires an extra fork, but is pretty much
  686.       completely portable.
  687.  
  688. ------------------------------
  689.  
  690. Subject: How do I get lines from a pipe ... instead of only in larger blocks?
  691. From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  692. Date: Sun, 16 Feb 92 20:59:28 -0500
  693.  
  694. 3.14) How do I get lines from a pipe as they are written instead of only in
  695.       larger blocks?
  696.  
  697.       The stdio library does buffering differently depending on whether
  698.       it thinks it's running on a tty.  If it thinks it's on a tty, it
  699.       does buffering on a per-line basis; if not, it uses a larger
  700.       buffer than one line.
  701.  
  702.       If you have the source code to the client whose buffering you
  703.       want to disable, you can use setbuf() or setvbuf() to change the
  704.       buffering.
  705.  
  706.       If not, the best you can do is try to convince the program that
  707.       it's running on a tty by running it under a pty, e.g. by using
  708.       the "pty" program mentioned in question 3.9.
  709.  
  710. ------------------------------
  711.  
  712. End of unix/faq Digest part 3 of 7
  713. **********************************
  714.  
  715. -- 
  716. Ted Timar - tmatimar@isgtec.com
  717.  
  718.